package models.venue.coach;

import enums.GoodsType;
import enums.ProductStatus;
import jodd.bean.BeanCopy;
import models.BaseModel;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.venue.enums.GenderType;
import models.venue.fitness.Fitnesses;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.util.List;
import java.util.Map;

/**
 * 教练
 * Created by hanbin on 2017/4/5.
 */
@Entity
@Table(name="coach_infos")
public class Coach extends BaseModel {

    /**
     * 教练姓名
     */
    @Column(name = "coach_name", length = 255)
    public String coachName;

    /**
     * 性别
     */
    @Column(name = "gender_type")
    @Enumerated(EnumType.STRING)
    public GenderType genderType;

    /**
     * 图片
     */
    @Column(name = "coach_image")
    public String coachImage;

    /**
     * 所属店面
     */
    @JoinColumn(name="merchant_id")
    @ManyToOne
    public Merchant merchant;

    /**
     * 健身类型
     */
    @JoinColumn(name="fitness_id")
    @ManyToOne
    public Fitnesses fitnesses;

    /**
     * 描述
     */
    @Column(name = "remark", length = 255)
    public String remark;

    /**
     * 交易量
     */
    @Column(name = "trading_volume")
    public Integer tradingVolume;

    /**
     * 上午上课时间
     */
    @Column(name="class_time_am", length = 50)
    public String classTimeAM;

    /**
     * 上午课时数量
     */
    public Integer classNumberAM;

    /**
     * 下午上课时间
     */
    @Column(name="class_time_pm", length = 50)
    public String classTimePM;

    /**
     * 下午课时数量
     */
    public Integer classNumberPM;

    /**
     * 单次时长
     */
    @Column(name="single_time_length")
    public Integer singleTimeLength;

    /**
     * 课间间隔
     */
    @Column(name="time_interval")
    public Integer timeInterval;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 分页查询.
     */
    public static JPAExtPaginator<Coach> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        StringBuilder xsqlBuilder = new StringBuilder("t.deleted=models.constants.DeletedStatus.UN_DELETED ")
                .append("/~ and t.coachName like {coachName} ~/")
                .append("/~ and t.merchant.id = {merchantId} ~/");

        //if(conditionMap.get("payedOrder")!=null)xsqlBuilder.append(" and (t.status ="+OrderStatus.PAID+" or t.status="+OrderStatus.DISTRIBUTION+") ");

        /**
         *    Query query = t.id = {id};
         *    query.setPropery("id" , conditionMap.get("id"));
         */
        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator<Coach> orderPage = new JPAExtPaginator<Coach>("Coach t", "t", Coach.class,
                result.getXsql(), conditionMap).orderBy(orderByExpress);
        orderPage.setPageNumber(pageNumber);
        orderPage.setPageSize(pageSize);
        orderPage.setBoundaryControlsEnabled(false);
        return orderPage;
    }

    /**
     * Update
     * @param id
     * @param newObject
     */
    public static void update(Long id, Coach newObject) {
        Coach coach = Coach.findById(id);
        BeanCopy.beans(newObject, coach).ignoreNulls(true).copy();
        coach.save();
    }

    /**
     * 根据健身类型和商户查找教练
     * @return
     */
    public static List<Coach> findAllCoachesByFitTypeAndMerchant(final long id, long merchantId){
        return find("deleted = ? and fitnesses.id = ? and merchant.id = ? order by id",DeletedStatus.UN_DELETED, id, merchantId).fetch();
    }

}
